//
//  KLCrashViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/6/30.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLCrashViewController.h"
#import "KLHttpAPI.h"
#import "KLHelp.h"
#import "Masonry.h"
#import "KLCrashLoanViewController.h"
#import "HemaFunction.h"
#import "JRToast.h"
#import "FMDBTool.h"
#import "HemaFunction.h"

@interface KLCrashViewController ()<UITextFieldDelegate>
{
    UIView *fatherView_1;
    UIView *fatherView_2;
    
    UIButton *nextBtn;
    UITextField *countTF;
    UITextField *nameTF;
}
@end

@implementation KLCrashViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    countTF.delegate = self;
    nameTF.delegate = self;
    //初始化导航栏
    [self naveSet];
    //初始化主界面
    [self uiSet];
}

#pragma mark - 初始化导航栏
-(void)naveSet{
    self.navigationController.navigationBarHidden = NO;
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 初始化主界面
-(void)uiSet{
    [self setFatherView];
    //拿到登录信息的数据
    NSString *aliuser = [[NSUserDefaults standardUserDefaults] objectForKey:@"aliuser"];
    NSString *ali_nickname = [[NSUserDefaults standardUserDefaults] objectForKey:@"ali_nickname"];
    NSLog(@"aliuser:%@,ali_nickname:%@",aliuser,ali_nickname);
    //支付宝logo
    UIImageView *aliPayLogo = [[UIImageView alloc]init];
    aliPayLogo.image = [UIImage imageNamed:@"支付宝"];
    aliPayLogo.layer.masksToBounds = YES;
    aliPayLogo.layer.cornerRadius = 5.0f;
    [fatherView_1 addSubview:aliPayLogo];
    [aliPayLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fatherView_1).with.offset(0);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    //输入账户的textfield
    countTF = [[UITextField alloc]init];
    countTF.borderStyle = UITextBorderStyleNone;
    if([HemaFunction xfunc_check_strEmpty:aliuser]){
        countTF.placeholder = @"输入支付宝账号";
    }else{
        countTF.text = aliuser;
    }
    countTF.keyboardType = UIKeyboardTypeURL;
    [countTF becomeFirstResponder];
    [fatherView_1 addSubview:countTF];
    [countTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.centerY.equalTo(fatherView_1).with.offset(0);
        make.left.equalTo(aliPayLogo).with.offset(60);
        make.right.equalTo(fatherView_1).with.offset(0);
    }];
    
    //昵称提示label
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = @"昵称";
    nameLabel.font = [UIFont systemFontOfSize:18.0f];
    [fatherView_2 addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(fatherView_2).with.offset(0);
    }];
    //输入昵称的textfield
    nameTF = [[UITextField alloc]init];
    nameTF.borderStyle = UITextBorderStyleNone;
    if([HemaFunction xfunc_check_strEmpty:ali_nickname]){
        nameTF.placeholder = @"输入支付宝昵称";
    }else{
        nameTF.text = ali_nickname;
    }
    [fatherView_2 addSubview:nameTF];
    [nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel).with.offset(60);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(fatherView_2).with.offset(0);
        make.right.equalTo(fatherView_2).with.offset(0);
    }];
}

-(void)setFatherView{
    self.view.backgroundColor = RGB_UI_COLOR(238, 238, 238);
    UILabel *noticeLabel = [[UILabel alloc]init];
    noticeLabel.text = @"当前仅支持支付宝账号:";
    noticeLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:noticeLabel];
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    
    fatherView_1 = [[UIView alloc]init];
    fatherView_1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:fatherView_1];
    [fatherView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    fatherView_2 = [[UIView alloc]init];
    fatherView_2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:fatherView_2];
    [fatherView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(fatherView_1).offset(85);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    nextBtn = [[UIButton alloc]init];
    [nextBtn setBackgroundColor:RGB_UI_COLOR(8, 128, 181)];
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = 5.0f;
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(rechargeNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(fatherView_2).offset(75);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
}

#pragma mark - 下一步点击事件
-(void)rechargeNext:(id)sender{
    [self.view endEditing:YES];
    if([HemaFunction xfunc_check_strEmpty:countTF.text] || [HemaFunction xfunc_check_strEmpty:nameTF.text]){
        [JRToast showWithText:@"请填写完整信息"];
        return ;
    }
    [KLHelp showHUDAddedTo:self.view animated:YES];
    [KLHttpAPI saveUsersAliAccountWithAliuser:countTF.text aliNickName:nameTF.text completionHandler:^(MKNetworkOperation *operation) {
        [KLHelp hideAllHUDsForView:self.view animated:YES];
        NSLog(@"保存支付宝账号的返回信息:%@",operation.responseString);
        NSDictionary *dic = operation.responseJSON;
        NSString *isSuccess = [dic objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            KLCrashLoanViewController *crashLoanVC = [[KLCrashLoanViewController alloc]init];
            [self.navigationController pushViewController:crashLoanVC animated:YES];
        }else{
            [JRToast showWithText:[dic objectForKey:@"msg"]];
        }
    } errorHandler:^(NSString *error) {
        [KLHelp showHUDAddedTo:self.view animated:YES];
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [KLHelp hideAllHUDsForView:self.view animated:YES];
}

@end
